
import java.io.IOException;
import javax.microedition.lcdui.*;
import javax.microedition.lcdui.game.*;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author NgocTam
 */
public class HeroGame2 extends GameCanvas implements Runnable{
    private TiledLayer mWallLayer;
    private TiledLayer mFloorLayer;
    private Hero mHero;
    private LayerManager mLayerManager = new LayerManager();
    private String go = "left";
    private float sx = 165;
    private Midlet midlet;
    private Form menu;
    public AudioService audio;
    private float x1 = 10f;
    private float x2 = 1f;
    private Sprite mEnemy;
    private boolean isEnemyAlive = true;
    private int smove;
    int score = 10000;
    
    public HeroGame2(Midlet midlet, Form menu)
    {
        super(true);
        this.menu = menu;
        this.midlet = midlet;
        createGame();
        new Thread(this).start();
        audio = new AudioService("nhac_nen_herogame4.wav");
        audio.playFromResource();
    }
 private void createBackGround(Graphics g, Image im) {
        g = getGraphics();
        mWallLayer = new TiledLayer(44, 10, im, 32, 32);

        int[] map = {
             1,  1,  1,  1,  1,  1,  1,  1,200,  3,199, 8,  9,  10, 11, 13, 13, 13, 13,396,396,396,396,396,396,396,396, 37, 38, 39, 40, 41, 42, 43, 37, 38, 39, 40, 41, 42, 43,396,396,396,
             1,  2,  3,  4,  6,  6,  6,  4,200,  3,199, 30, 31, 32, 33, 34, 13, 12, 13,138, 36, 36, 35,396,396,396,396, 59, 60, 61, 62, 63, 64, 65, 59, 60, 61, 62, 63, 64, 65,396,396,396,
            23, 24, 25, 26, 27, 28, 29, 29,200,  3,199, 52, 53, 54, 55, 56, 57, 13, 13,396, 58, 58,396,396,396,396,396, 81, 82, 83, 84, 85, 86, 87, 81, 82, 83, 84, 85, 86, 87,396,396,396,
            67, 68, 69, 70, 71, 72, 73, 73,200,  3,199, 74, 75, 76, 77, 78, 79, 13, 13,396, 58, 58,396,228,229,229,230,103,104,105,106,107,108,109,107,106,105,104,103,228,229,229,229,230,
            89, 90, 91, 92, 93, 94,  1,199,200,  3,199, 96, 97, 98, 77, 78, 13, 13, 13,396, 58, 58,396,228,229,229,230,103, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12,228,229,229,229,230,
           111,111, 92, 93, 94, 71, 69,199,200,  3,199,118,119,120,121,122, 13, 13, 13,396, 58, 58,396,228,229,229,230,103, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12,228,229,229,229,230,
             1,  1,  1,  1, 70, 70, 69,199,200,  3,199, 12, 12, 12, 12, 12, 12, 12, 12,396, 58, 58,396, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12,
            12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12,
            12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12,
            12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12};

        for (int i = 0; i < map.length; i++) {
            int column = i % 44;
            int row = (i - column) / 44;
            mWallLayer.setCell(column, row, map[i]);
        }
        mLayerManager.append(mWallLayer);
        mLayerManager.paint(g, 0, 0);
    }

    private void createFloor(Graphics g, Image im) {
        mFloorLayer = new TiledLayer(44, 10, im, 32, 32);
        int[] map = {
            0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
            0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
            0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
            0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
            0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,101,  0,  0,  0,  0,  0,101,  0,  0,  0,221,222,125,126,127,128,129,  0,  0,129,128,127,126,125,222,221,  0,  0,  0,
            0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0, 99,100,101,  0,  0,  0,  0,100,101,  0,221,222,223,147,148,149,150,151,  0,  0,151,150,149,148,147,223,222,221,  0,  0,
            0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0, 99,100,101, 99, 99,100,101,  0,  0, 99,100,101,221,222,223,224, 81, 82, 83, 84, 85, 86, 87, 81, 82, 83, 84, 85, 86,224,223,222,221,
            4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4, 81, 82, 83, 84, 85, 86, 87, 81, 82, 83, 84, 85, 86,  4,  4,  4,  4,
           81, 82, 83, 84, 85, 86, 87, 81, 82, 83, 84, 85, 86, 87, 81, 82, 83, 84, 85, 86, 87, 81, 82, 83, 84, 85, 86, 87, 81, 82, 83, 84, 85, 86, 87, 81, 82, 83, 84, 85, 86, 87, 81, 82,
          103,104,105,106,107,108,109,103,104,105,106,107,108,109,103,104,105,106,107,108,109,103,104,105,106,107,108,109,103,104,105,106,107,108,109,103,104,105,106,107,108,109,103,104};
        for (int i = 0; i < map.length; i++) {
            int column = i % 44;
            int row = (i - column) / 44;
            mFloorLayer.setCell(column, row, map[i]);
        }
        mLayerManager.append(mFloorLayer);
    }

    private void createHero(Graphics g) {
        try {
            Image im = Image.createImage("/Hero.png");
            mHero = new Hero(im, 23, 41);
            mHero.setFrame(mHero.framefomove);
            mHero.setPosition(50, 80);
            mHero.defineReferencePixel(12, 21);
            mLayerManager.append(mHero);

        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
    private TiledLayer mFire;

    private void createSea(Graphics g, Image im) {
       
        mFire = new TiledLayer(44, 10, im, 32, 32);
        int[] map = {
            0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
            0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
            0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
            0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
            0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,130,131, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
            0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,152,153, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
            0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
            0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
            0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
            0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};

        for (int i = 0; i < map.length; i++) {
            int column = i % 44;
            int row = (i - column) / 44;
            mFire.setCell(column, row, map[i]);
        }
        mLayerManager.append(mFire);
    }

    private void createGame() {
        try {
            Graphics g = getGraphics();
            Image im = Image.createImage("/tiles.png");
            createHero(g);
            createEnemy(g);
            createSea(g, im);
            createFloor(g, im);
            createBackGround(g, im);
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

    public void run() {
        while (true) {
            try {
                Graphics g = getGraphics();
                int ox = mHero.getX();
                int oy = mHero.getY();

                if (mHero.isHeroAlive) {
                    mHero.setPosition(ox, oy);
                    int key = getKeyStates();
                    if ((key & LEFT_PRESSED) != 0) {
                        mHero.setFrame(0 + mHero.framefomove++);
                        mHero.setTransform(Sprite.TRANS_NONE);
                        mHero.move(-1, 0);
                        if (mHero.framefomove == 12) {
                            mHero.framefomove = 9;
                        }
                    } else if ((key & RIGHT_PRESSED) != 0) {
                        mHero.setFrame(0 + mHero.framefomove++);
                        mHero.move(1, 0);
                        mHero.setTransform(Sprite.TRANS_MIRROR);
                        if (mHero.framefomove == 12) {
                            mHero.framefomove = 9;
                        }
                    }
                    if ((key & UP_PRESSED) != 0 && mHero.jump == 0) {
                        mHero.jump = 120;
                        x1=10;
                        mHero.framefomove = 9;
                    }
                    if (mHero.jump > 0) {
                        if (mHero.jump > 30) {
                            mHero.setPosition(mHero.getX(), mHero.getY() - (int)x1);
                            mHero.jump--;
                            x1=x1*0.9f;
                        }
                    }

                    if (mHero.getX() < 0) {
                        mHero.setPosition(0, mHero.getY());
                    }

                    if ((mHero.getX() > 170)) {
                        mHero.setPosition(ox, oy);
                        System.out.println(""+mWallLayer.getX());
                        if ((mWallLayer.getX() > -810) && (mWallLayer.getX() > -800)) {
                            //mHero.setPosition(mHero.getX() + 1, mHero.getY());
                            mWallLayer.move(-3, 0);
                            mFloorLayer.move(-3, 0);
                            mFire.move(-3, 0);
                        } else {
                            mHero.isTranmitted = false;
                        }
                        //mEnemy.move(-3, 0);

                    }


                    if (mHero.collidesWith(mFloorLayer, true)) {
                        mHero.jump = 0;
                        mHero.setPosition(ox, oy);
                        System.out.println("collide");
                    } else {
                        mHero.setPosition(mHero.getX(), mHero.getY() + (int)x2);
                        x2=x2*1.03f;
                        while (mHero.collidesWith(mFloorLayer, true)) {
                            mHero.setPosition(mHero.getX(), mHero.getY() - 1);
                            mHero.jump = 0;
                            
                        x2=1.0f;    
                        }
                        
                    }
                    

                    if (mHero.collidesWith(mFire, true)) {
                        mHero.isHeroAlive = false;
                    }
                    
                    //Enemy Move
                    if (isEnemyAlive) {
                        if ("left".equals(go)) {
                            sx = sx - 0.3f;
                            mEnemy.setPosition((int) sx, mEnemy.getY());
                        } else {
                            sx = sx + 0.3f;
                            mEnemy.setPosition((int) sx, mEnemy.getY());
                        }
                        if (mEnemy.getX() > 165) {
                            go = "left";
                        }
                        if (mEnemy.getX() < 20) {
                            go = "right";
                        }
                        if (smove == 1) {
                            smove = 0;
                        } else {
                            smove = 1;
                        }
                    }

                    if (mHero.collidesWith(mEnemy, true) && isEnemyAlive == true) {
                        if (mHero.getY() < mEnemy.getY() - 10) {
                            mEnemy.setFrame(2);
                            isEnemyAlive = false;
                            mLayerManager.remove(mEnemy);
                        } else {
                            mHero.isHeroAlive = false;
                            mHero.setFrame(9);
                        }
                    }
                    mLayerManager.paint(g, 0, 0);
                    
                } else {
                    audio.stop();
                    midlet.createGameOver();
                    break;
                }
                if (mHero.isTranmitted == false) {
                    audio.stop();
                    midlet.createTranmitted();
                    break;
                }
                mLayerManager.paint(g, 0, 0);
                
                g.setColor(255, 255, 255);
                g.setFont(Font.getFont(Font.FACE_SYSTEM, Font.STYLE_BOLD, Font.SIZE_LARGE));
                g.drawString(String.valueOf(score), 20, 30, g.TOP|g.LEFT);
                score --;
                    
                flushGraphics();
                Thread.sleep(7);
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
    }

    public void commandAction(Command c, Displayable d) {
    }

    private void createEnemy(Graphics g) {
        try {
            Image im = Image.createImage("/enemy.png");
            mEnemy = new Sprite(im, 25, 25);
            mEnemy.setPosition((int) sx, 203);
            mEnemy.defineReferencePixel(13, 13);
            mLayerManager.append(mEnemy);
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
}
